/*
 * JStock - Free Stock Market Software
 * Copyright (C) 2011 Yan Cheng CHEOK <yccheok@yahoo.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.yccheok.jstock.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import org.yccheok.jstock.engine.StockInfo;

/**
 * A renderer for AutoCompleteJComboBox to display stock info.
 * It will display code at left side, and display symbol at right side.
 * @author yccheok
 */
public class StockInfoCellRenderer extends javax.swing.JPanel implements ListCellRenderer {

    private final DefaultListCellRenderer jLabel1 = new DefaultListCellRenderer();
    private final DefaultListCellRenderer jLabel2 = new DefaultListCellRenderer();

    /** Creates new form StockInfoCellRenderer */
    public StockInfoCellRenderer() {
        initComponents();
        this.add(jLabel1, BorderLayout.WEST);
        // Is having 88 magic number a good idea?
        jLabel1.setPreferredSize(new Dimension(88, jLabel1.getWidth()));
        this.add(jLabel2, BorderLayout.CENTER);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        final StockInfo stockInfo = (StockInfo)value;
        // getListCellRendererComponent will update internal state of JLabel.
        jLabel1.getListCellRendererComponent(list, stockInfo.code, index, isSelected, cellHasFocus);
        jLabel2.getListCellRendererComponent(list, stockInfo.symbol, index, isSelected, cellHasFocus);

        return this;
    }
}
